﻿#include "StdAfx.h"
#include "Game.h"

#include "D3DX.h"
#include "D3DXRender.h"
#include "D3DXDefines.h"
#include "Terrain.h"
#include "Camera.h"
#include "AseUtil.h"
#include "AseObject.h"
#include "Triangle.h"

CGame::CGame(HWND hwnd,int width,int height)
:mHwnd(hwnd)
,mGameRun(true)
,mDx(NULL)
,mDXRenderer(NULL)
,mCamera(NULL)
,mObjectList()
,mMouseEventListener()
,mWindoWidth(width)
,mWindoHeight(height)
{}

CGame::~CGame(void)
{
	for (ObjectList::iterator it=mObjectList.begin(); it != mObjectList.end(); ++it)
	{
		delete *it;
	}

	delete mDXRenderer;
	delete mDx;
}

bool CGame::Init()
{
	// dx init
	mDx = new D3DX;
	mDx->Init(mHwnd);

	//Renderer
	mDXRenderer = new D3DXRenderer(mDx);

	//camera
	mCamera = new Camera(this);
	mObjectList.push_back(mCamera);

	//======================================================//
	//여기서 오브젝트 추가

	// 지형
	/*Terrain* terrain = new Terrain(this);
	mObjectList.push_back( terrain );
	terrain->Init(2, 2, 8, 8, 2.0f);
	mDXRenderer->AddRenderListener( terrain );*/

	//삼각형 오브젝트 (CTriangle)
	CTriangle* triangle = new CTriangle(this);
	mObjectList.push_back( triangle );
	mDXRenderer->AddRenderListener( triangle );
	
	return true;
}

bool CGame::Tick( float time )
{
	for ( ObjectList::iterator it = mObjectList.begin(); it != mObjectList.end(); ++it )
	{
		(*it)->Tick();
	}
	return true;
}

bool CGame::Render()
{
	mDXRenderer->GatherRenderData();

	mCamera->SetMatrices();

 	for ( ObjectList::iterator it = mObjectList.begin(); it != mObjectList.end(); ++it )
 	{
 		(*it)->PrepareRender();
 	}

	mDXRenderer->Render();

	return true;
}

void CGame::AddMouseEventListener( MouseEventListener* listener)
{
	mMouseEventListener.push_back( listener );
}

void CGame::DelMouseEventListener( MouseEventListener* listener)
{
	// http://jerevinan21.tistory.com/108
	mMouseEventListener.erase( std::remove(mMouseEventListener.begin(),mMouseEventListener.end(),listener), mMouseEventListener.end() );
}

void CGame::ProcessMouseEventMove( MouseEvent& evt )
{
	std::vector< MouseEventListener* >::iterator it;
	for ( it = mMouseEventListener.begin(); it != mMouseEventListener.end(); ++it )
	{
		(*it)->MouseEventMove( evt );
	}
}

void CGame::ProcessMouseEventClick( MouseEvent& evt )
{
	std::vector< MouseEventListener* >::iterator it;
	for ( it = mMouseEventListener.begin(); it != mMouseEventListener.end(); ++it )
	{
		(*it)->MouseEventClick( evt );
	}
}

void CGame::ProcessMouseEventWheel( MouseEvent& evt )
{
	std::vector< MouseEventListener* >::iterator it;
	for ( it = mMouseEventListener.begin(); it != mMouseEventListener.end(); ++it )
	{
		(*it)->MouseEventWheel( evt );
	}
}

//기자가 정보를 받아왔다
void CGame::ProcessKeyEvent( KeyEvent& evt )
{
	// 게임종료
	// 너네 신문회사 망함ㅋ
	if ( evt._key == VK_ESCAPE && evt._button == KEY_UP )
	{
		SetGameRun(false);
	}

	// 나중에 키 리스너에 메세지 분배
	// 특종이다! 어서 신문을 배포하게
	std::vector< KeyEventListener* >::iterator it;
	for ( it = mKeyEventListener.begin(); it != mKeyEventListener.end(); ++it )
	{
		//고갱님 1,2,3에게 신문을 전달하라
		(*it)->KeyEventMsg( evt );
	}
}

void CGame::AddKeyEventListener( KeyEventListener* listener )
{
	mKeyEventListener.push_back( listener );
}

void CGame::DelKeyEventListener( KeyEventListener* listener )
{
	// http://jerevinan21.tistory.com/108
	mKeyEventListener.erase( std::remove(mKeyEventListener.begin(),mKeyEventListener.end(),listener), mKeyEventListener.end() );
}
